package com.cwt_taxi_xa;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.can.meter.IViewMeter;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;

public class ActivityMeter extends CWTBaseActivity implements IViewMeter ,SeekBar.OnSeekBarChangeListener{

	final static String TAG = "ActivityFMConfig";

	TextView txFreightRate,txTotalMileage,txTiming,txTotalPrice,tvVehicleSpeed,tvTotalMileage;
	TextView tvPeopleNumber,tvNumberThreshold;
	SeekBar mVehicleSpeed,sbTotalMileage,sbPeopleNumber;
	Switch swtHalf;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		openSwipeBack(true);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aa_meter);
		AppLog.d(TAG, "onCreate");

		txFreightRate = (TextView)findViewById(R.id.tv_freight_rate);//运价
		txTotalMileage = (TextView)findViewById(R.id.tv_total_mileage);//总里程
		txTiming = (TextView)findViewById(R.id.tv_timing);//计时
		txTotalPrice = (TextView)findViewById(R.id.tv_total_price);//总价格
		tvPeopleNumber = (TextView)findViewById(R.id.tv_people_number);//人数

		MCUUtil.mVehicleInformation.softWareMeter.register(this);

		tvVehicleSpeed = (TextView)findViewById(R.id.tv_vehicle_speed);
		mVehicleSpeed = (SeekBar)findViewById(R.id.sb_vehicle_speed);
		//车速
		mVehicleSpeed.setOnSeekBarChangeListener(this);
		mVehicleSpeed.setMax(200);
		mVehicleSpeed.setProgress((int)MCUUtil.mVehicleInformation.getVehicleSpeed());
		tvVehicleSpeed.setText((int)MCUUtil.mVehicleInformation.getVehicleSpeed() + "");
		tvTotalMileage = (TextView)findViewById(R.id.tv_can_total_mileage);
		sbTotalMileage = (SeekBar)findViewById(R.id.sb_total_mileage);
		//人脸识别阈值
		tvPeopleNumber= (TextView)findViewById(R.id.tv_people_number);
		tvNumberThreshold = (TextView)findViewById(R.id.tv_people_number_threshold);
		sbPeopleNumber = (SeekBar)findViewById(R.id.sb_people_number_threshold);
		sbPeopleNumber.setOnSeekBarChangeListener(this);
		sbPeopleNumber.setMax(SPConstants.SPV_threshold_max);
		sbPeopleNumber.setProgress(SPUtils.getParamInteger(SPConstants.SPK_people_threshold,SPConstants.SPV_threshold_max/2));
		tvNumberThreshold.setText((sbPeopleNumber.getProgress() + SPConstants.SPV_threshold_default) + "");
		//人脸识别阈值

		sbTotalMileage.setOnSeekBarChangeListener(this);
		sbTotalMileage.setMax(200);
		mVehicleSpeed.setProgress(MCUUtil.mVehicleInformation.getTotalMileage());

		swtHalf = (Switch)findViewById(R.id.sth_half);
		swtHalf.setChecked(MCUUtil.mVehicleInformation.softWareMeter.isHalfWayBilling());
		swtHalf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				MCUUtil.mVehicleInformation.softWareMeter.setHalfWayBilling(isChecked);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AppLog.d(TAG,"onResume");

	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		MCUUtil.mVehicleInformation.softWareMeter.unregister(this);
	}

	@Override
	public void setStatus(int index, boolean status) {

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"sendMsg type:" + type);

	}


	@Override
	public void freightRate(float value) {
		txFreightRate.setText(String.valueOf(value));
	}

	@Override
	public void totalMileage(int value) {
		txTotalMileage.setText(String.valueOf(value));
	}

	@Override
	public void timing(String value) {
		txTiming.setText(value);
	}

	@Override
	public void totalPrice(double value) {
		txTotalPrice.setText(String.format("%.2f", value));
	}

	@Override
	public void totalPeople(int value) {
		tvPeopleNumber.setText(value + "");
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		switch (seekBar.getId()){
			case R.id.sb_vehicle_speed:
				tvVehicleSpeed.setText(progress + "");
				break;
			case R.id.sb_total_mileage:
				tvTotalMileage.setText(progress + "");
				break;
			case R.id.sb_people_number_threshold:
					tvNumberThreshold.setText((progress + SPConstants.SPV_threshold_default) + "");
				break;

		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		switch (seekBar.getId()){
			case R.id.sb_vehicle_speed:
				MCUUtil.mVehicleInformation.setVehicleSpeed(seekBar.getProgress());
				tvVehicleSpeed.setText(seekBar.getProgress() + "");
				break;
			case R.id.sb_total_mileage:
				MCUUtil.mVehicleInformation.setTotalMileage(seekBar.getProgress());
				tvTotalMileage.setText(seekBar.getProgress()  + "");
				break;
			case R.id.sb_people_number_threshold:
				SPUtils.setParam(SPConstants.SPK_people_threshold,seekBar.getProgress());
				tvNumberThreshold.setText((seekBar.getProgress() + SPConstants.SPV_threshold_default) + "");
				break;
		}
	}
}
